package com.mycompany;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class TimeTaskService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    // 格式化时间显示
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public void startCountdownTask() {
        threadPoolExecutor.submit(() -> {
            int seconds = 20; // 倒计时60秒
            while (seconds > 0) {
                ZonedDateTime now = ZonedDateTime.now();
                System.out.printf("当前时间: %s, 倒计时剩余: %d秒%n",
                        now.format(FORMATTER), seconds);
                try {
                    Thread.sleep(1000); // 每秒执行一次
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // 恢复中断状态
                    break;
                }
                seconds--;
            }
            System.out.println("倒计时结束!");
        });
    }
}
